.PHONY : clean qemu all debug fs sdcard

K = kernel
I = include
R = result
B = bin

INCLUDE = ./include/

mode := debug
#mode := release
platform := qemu

SRCS_ASM = \
	./$(K)/start_qemu.S \
	./$(K)/contentSwitch.S \
	./$(K)/trampoline.S \
	./$(K)/vec.S 

SRCS_C = \
	./$(K)/main.c \
	./$(K)/console.c \
	./$(K)/mmu.c \
	./$(K)/proc.c \
	./$(K)/stdio.c \
	./$(K)/string.c \
	./$(K)/syscall.c \
	./$(K)/trap.c    \
	./$(K)/time.c 	\
	./$(K)/plic.c \
	./$(K)/sysproc.c  \
	./$(K)/sysmm.c \
	./$(K)/lock/monitor.c \
	./$(K)/lock/spinlock.c \
	./$(K)/lock/sleeplock.c \
	./$(K)/lock/semaphore.c \
	./$(K)/lock/wait.c \
	./$(K)/fs/fat32.c \
	./$(K)/fs/file.c \
	./$(K)/fs/disk.c \
	./$(K)/fs/sdcard.c \
	./$(K)/fs/gpiohs.c \
	./$(K)/fs/sysfile.c \
	./$(K)/fs/sysctl.c \
	./$(K)/fs/virtio.c \
	./$(K)/fs/fpioa.c \
	./$(K)/fs/spi.c \
	./$(K)/fs/iobuf.c \
	./$(K)/fs/dmac.c 

OBJS_O = $(SRCS_ASM:.S=.o)
OBJS_O += $(SRCS_C:.c=.o)
# OBJS := $(subst $(K),$(B),$(OBJS_))
OBJS_D := $(OBJS_O:.o=.d)
OBJS := $(OBJS_O)

OUTPUT := $(OBJS)
# OUTPUT += $(OBJS_D)


# QEMU = /opt/qemu/qemu7.1/bin/qemu-system-riscv64
QEMU = qemu-system-riscv64
OPENSBI = ./boot_loader/qemu/fw_jump.bin


QEMUOPTS = -machine virt -kernel os.bin -m 16M -nographic
QEMUOPTS += -smp 1
QEMUOPTS += -drive file=disk.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

# TOOLPREFIX := riscv64-unknown-linux-gnu-
# TOOLPREFIX := /opt/riscv-new/bin/riscv64-unknown-elf-
TOOLPREFIX := riscv64-unknown-elf-
CC = $(TOOLPREFIX)gcc
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy

CFLAGS = -mcmodel=medany -W
CFLAGS += -nostdlib
#CFLAGS += -O -fno-omit-frame-pointer -ggdb -ffreestanding -fno-common -mno-relax 
#CFLAGS += -z max-page-size=4096
CFLAGS += -Wall -fno-omit-frame-pointer #-ggdb -MD 
CFLAGS += -ffreestanding -fno-common -mno-relax


CFLAGS += -I${INCLUDE}


ifeq ($(mode), debug)
CFLAGS += -g3 -O0
CFLAGS += -DDEBUG 
QEMUOPTS += -gdb tcp::1234 -S
else
CFLAGS += -O3
endif

ifeq ($(platform), qemu)
CFLAGS += -DQEMU
LINKER = ./linker/qemu.ld 
SBI = ./boot_loader/qemu/rustsbi-qemu.bin
else
LINKER = ./linker/k210.ld 
SBI = ./boot_loader/k210/rustsbi-k210
endif

QEMUOPTS += -bios $(SBI) # TODO

all: os.bin

k210-serialport := /dev/ttyUSB0

os.bin: kernel.elf
ifeq ($(platform), qemu)
	$(OBJCOPY) -O binary kernel.elf os.bin
else
	$(OBJCOPY) kernel.elf --strip-all -O binary kernel.bin
	$(OBJCOPY) $(SBI) --strip-all -O binary os.bin
	dd if=kernel.bin of=os.bin bs=128k seek=1
# ifeq ($(mode), debug)
# 	sudo chmod 777 $(k210-serialport)
# 	python3 kflash.py -p $(k210-serialport) -b 1500000 -t os.bin
# endif

endif

run: os.bin
ifeq ($(mode), debug)
	sudo chmod 777 $(k210-serialport)
	python3 kflash.py -p $(k210-serialport) -b 1500000 -t os.bin
endif

kernel.elf: $(OBJS)
	${CC} ${CFLAGS} -Tlinker/qemu.ld -o kernel.elf $^

%.o : %.c 
	${CC} ${CFLAGS} -c -o $@ $< 

%.o : %.S 
	${CC} ${CFLAGS} -c -o $@ $<

qemu: os.bin
#gdb --args $(QEMU) -kernel kernel.elf $(QEMUOPTS) 
	$(QEMU) -kernel kernel.elf $(QEMUOPTS) 

# disk.img: fs
# dst=/home/anto/oskernel/oskernel2021-ustc
dst=/mnt/oskernel
qemu-disk:
	if [ ! -f "disk.img" ]; then \
		echo "making fs image..."; \
		dd if=/dev/zero of=disk.img bs=512k count=512; \
		mkfs.vfat -F 32 disk.img; fi
	sudo mount disk.img $(dst)
	for file in $$( ls riscv64/ ); do \
		sudo cp -r riscv64/$${file} $(dst)/$${file};\
		done
	sudo umount $(dst)

# sudo cp $$file $(dst)/bin/$${file#$U/_}; done

#sdcard_dst:=/media/wangzhen0518/K210#需要根据具体情况进行更改
sdcard_dst:=/mnt/e#需要根据具体情况进行更改
sdcard:
	if [ ! -d "$(sdcard_dst)" ]; then sudo mkdir $(sdcard_dst); fi
	for file in $$( ls riscv64 ); do \
		sudo cp -r riscv64/$${file} $(sdcard_dst)/$${file}; \
		done
#@sudo cp $U/_init $(sdcard_dst)/init
#@sudo cp $U/_sh $(sdcard_dst)/sh
#@sudo cp README $(sdcard_dst)/README

clean:
	rm -f $(OUTPUT) kernel.elf os.bin kernel.bin